package com.aitaoxue.palx.db;

import android.content.Context;

import com.aitaoxue.palx.bean.UserInfos;

public class RongyunContext {

	private static RongyunContext mRongyunContext;
	private Context mContext;
	private UserInfosDAO mUserInfosDao;

	public static RongyunContext getInstance() {
		if (mRongyunContext == null) {
			mRongyunContext = new RongyunContext();
		}
		return mRongyunContext;
	}

	private RongyunContext() {

	}

	private RongyunContext(Context context) {
		mContext = context;
		mRongyunContext = this;

		mUserInfosDao = RongyunDBManager.getInstance(context).getDaoSession()
				.getUserInfosDao();
	}

	public static void init(Context context) {
		mRongyunContext = new RongyunContext(context);
	}

	public void deleteUserInfos() {
		mUserInfosDao.deleteAll();
	}

	public void updateUserInfos(String targetid, String status) {
		UserInfos userInfos = mUserInfosDao.queryBuilder()
				.where(UserInfosDAO.Properties.Userid.eq(targetid)).unique();
		userInfos.setUsername(userInfos.getUsername());
		userInfos.setPortrait(userInfos.getPortrait());
		userInfos.setUserid(userInfos.getUserid());

		mUserInfosDao.update(userInfos);
	}

	public void insertOrReplaceUserInfo(UserInfos info) {
		UserInfos userInfos = new UserInfos();
		userInfos.setUsername(info.getUsername());
		userInfos.setPortrait(String.valueOf(info.getPortrait()));
		userInfos.setUserid(info.getUserid());
		mUserInfosDao.insertOrReplace(userInfos);
	}

	public UserInfos getUserInfoById(String userId) {
		if (userId == null) {
			return null;
		}
		UserInfos userinfos = mUserInfosDao.queryBuilder()
				.where(UserInfosDAO.Properties.Userid.eq(userId)).unique();
		if (userinfos == null && RongyunContext.getInstance() != null) {
			return null;
		}

		return userinfos;
	}

	public UserInfosDAO getmUserInfosDao() {
		return mUserInfosDao;
	}

	public void setmUserInfosDao(UserInfosDAO mUserInfosDao) {
		this.mUserInfosDao = mUserInfosDao;
	}

}
